//
//  AwemeDylib.m
//  AwemeDylib
//
//  Created by ted zhang on 2020/4/1.
//  Copyright (c) 2020 ted zhang. All rights reserved.
//

#import "AwemeDylib.h"
#import <CaptainHook/CaptainHook.h>
#import <Cycript/Cycript.h>
#import <MDCycriptManager.h>
#import "MenuViewController.h"

#define ROOTVIEW [[[UIApplication sharedApplication] keyWindow] rootViewController]

CHConstructor{
    printf(INSERT_SUCCESS_WELCOME);
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
#ifndef __OPTIMIZE__
        CYListenServer(6666);

        MDCycriptManager* manager = [MDCycriptManager sharedInstance];
        [manager loadCycript:NO];

        NSError* error;
        NSString* result = [manager evaluateCycript:@"UIApp" error:&error];
        NSLog(@"result: %@", result);
        if(error.code != 0){
            NSLog(@"error: %@", error.localizedDescription);
        }
#endif
        
    }];
}


CHDeclareClass(AWEAccountPrivacyAndUserItemAlertManager)
CHDeclareClass(AppDelegate)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"


#pragma clang diagnostic pop

CHOptimizedMethod1(self, void, AWEAccountPrivacyAndUserItemAlertManager, showIfNeededAndDecideHandler, id, block){
    // Call original function
    CHSuper1(AWEAccountPrivacyAndUserItemAlertManager, showIfNeededAndDecideHandler, block);
    
    NSLog(@"HOOK POPUP WINDOWS.");
    
    UIAlertController* alertView = [UIAlertController alertControllerWithTitle:@"张总有话说" message:@"\t历史犹如人生,业已失去的瞬间不会因为抱憾的心情而重返,仅仅一个小时所贻误的东西,用千年的时光也难以赎回。\n\n摘自《千年帝国的陨落》- 茨威格" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alertView addAction:cancelAction];
    [ROOTVIEW presentViewController:alertView animated:YES completion:nil];
    
    //MenuViewController* controller = [[MenuViewController alloc] init];
    //[ROOTVIEW presentViewController:controller animated:YES completion:^{}];

    
    /*
    CGRect viewRect = CGRectMake(10, 200, 360, 80);
    
    CGRect bounds = [[UIScreen mainScreen] bounds];

    UIWindow* window = [[UIWindow alloc] initWithFrame:bounds];
    window.windowLevel = UIWindowLevelAlert + 2;
    [window setHidden:NO];
    [window setAlpha:1.0];
    [window setBackgroundColor:[UIColor cyanColor]]; // nothing effert in here.

    UITextView* textView = [[UITextView alloc] initWithFrame: [window bounds]];
    [textView setText:@"抖音已经被注入!"];
    [textView setBackgroundColor: [UIColor cyanColor]];
    [textView setTextColor: [UIColor redColor]];
    [textView setFont: [UIFont boldSystemFontOfSize:25]];
    [textView setTextAlignment: NSTextAlignmentCenter];
    [window addSubview:textView];
     */
    
    //CGRect bounds = [[UIScreen mainScreen] bounds];
    //MenuViewController* controller = [[MenuViewController alloc] init];
    //UIView* view = [controller view];
    
    //id ctrl = [[UIApplication sharedApplication] delegate];
    //UIWindow* window = [ctrl window];
    
    //[window addSubview:view];
    
    
    /*
    UIAlertController* alertView = [UIAlertController alertControllerWithTitle:@"NOTE" message:@"hooked." preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alertView addAction:cancelAction];
    [myview presentViewController:alertView animated:YES completion:nil];
     */
    
    //CGRect bounds = [[UIScreen mainScreen] bounds];
    //AWEAccountPrivacyAndUserItemAlertView* myView = [[AWEAccountPrivacyAndUserItemAlertView alloc] initWithFrame:bounds];
    //[myView.titleLabel setText:@"张总有话说"];
    //NSString* myVoice = @"历史犹如人生,业已失去的瞬间不会因为抱憾的心情而重返,仅仅一个小时所贻误的东西,用千年的时光也难以赎回。";
    //NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:myVoice];
    //[myView.textLabel setAttributedText:attrStr];
    //[myView show];
     
}


CHOptimizedMethod2(self, BOOL, AppDelegate, application, id, application, didFinishLaunchingWithOptions, id, launchOptions) {
    BOOL bRet = CHSuper2(AppDelegate, application, application, didFinishLaunchingWithOptions, launchOptions);
    
    NSLog(@"[HOOK POINT] Start Application.");
    
    // Insert your code here, this is first time to initialize your feature data when begin app running.
    // You can display alerts or custom views here, but note that you can only choose either scenario.
#if 0
    UIAlertController* alertView = [UIAlertController alertControllerWithTitle:@"HOOK POINT" message:@"Insert the code here, which is the first time to initialize the functional data when you start running the application. \n\nIf you encounter an error or you really don't know how to do your work, please let me know. You can contact me at (86) 17712859115 or email me at kuku_@163.com. --Ted" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alertView addAction:cancelAction];
    [ROOTVIEW presentViewController:alertView animated:YES completion:nil];
#else
    MenuViewController* controller = [[MenuViewController alloc] init];
    [ROOTVIEW presentViewController:controller animated:YES completion:^{}];
#endif
    
    return bRet;
}


CHOptimizedMethod1(self, void, AppDelegate, applicationWillTerminate, id, application) {
    
    NSLog(@"[HOOK POINT] Terminate Application.");
    
    /* Insert your code here, this is last chance to free your feature data when ending app running.
     * If you encounter some error or you real do not how to do your job, please let me know,
     * you can contact me by (86)17712859115 or send email to me by kuku_998@163.com.
     */
    
    // The application will terminate before showing the alert window, so the following code isn't work.
    UIAlertController* alertView = [UIAlertController alertControllerWithTitle:@"HOOK POINT" message:@"Insert the code here, which is last chance to free your functional data when you end running the application. \n\nIf you encounter an error or you really don't know how to do your work, please let me know. You can contact me at (86) 17712859115 or email me at kuku_@163.com. --Ted" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [alertView addAction:cancelAction];
    [ROOTVIEW presentViewController:alertView animated:YES completion:nil];
    
    CHSuper1(AppDelegate, applicationWillTerminate, application);
}
 

CHConstructor{
    // linkable
    CHLoadLateClass(AWEAccountPrivacyAndUserItemAlertManager);
    
    CHClassHook1(AWEAccountPrivacyAndUserItemAlertManager, showIfNeededAndDecideHandler);
    
    // AppDelegate
    CHLoadLateClass(AppDelegate);
    CHClassHook2(AppDelegate, application, didFinishLaunchingWithOptions);
    CHClassHook1(AppDelegate, applicationWillTerminate);
}

